package com.ruoyi.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AddGroup;
import com.ruoyi.system.domain.Plaza;
import com.ruoyi.system.service.AddGroupService;
import com.ruoyi.system.service.PlazaService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： PlazaController
 *  创建时间：2025/2/11
 */
@RestController
@RequestMapping("/Plaza")
public class PlazaController {

    @Resource
    private PlazaService plazaService;

    //查询
    @PostMapping("/get/Plaza")
    public AjaxResult getPlaza(@RequestBody Plaza plaza){
        //创建分页对象
        Page<Plaza> page = new Page<>();
        //设置页码
        page.setCurrent(plaza.getCurrent());
        //设置行数
        page.setSize(plaza.getSize());
        //执行查询
        plazaService.page(page,new LambdaQueryWrapper<Plaza>()
                //群聊名称
                .eq(Objects.nonNull(plaza.getUserId()),Plaza::getUserId,plaza.getUserId())
                //审核状态
                .eq(Objects.nonNull(plaza.getStatus()),Plaza::getStatus,plaza.getStatus())
                //逻辑删除
                .eq(Plaza::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Plaza::getCreateTime)
                //添加分页语句
                .last(" limit " + (plaza.getCurrent() - 1) * plaza.getSize() +"," + plaza.getSize())
        );
        page.setTotal(plazaService.count(new LambdaQueryWrapper<Plaza>()
                //群聊名称
                .eq(Objects.nonNull(plaza.getUserId()),Plaza::getUserId,plaza.getUserId())
                //审核状态
                .eq(Objects.nonNull(plaza.getStatus()),Plaza::getStatus,plaza.getStatus())
                //逻辑删除
                .eq(Plaza::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Plaza::getCreateTime)
        ));
        //返回结果
        return AjaxResult.success(page);
    }

    //删除
    @PostMapping("/delete/Plaza/{id}")
    public AjaxResult deletePlaza(@PathVariable(value = "id")Long id){
        //调用Service层删除
        plazaService.update(null,new UpdateWrapper<Plaza>().lambda()
                //主键id
                .eq(Plaza::getId,id)
                //设置逻辑删除为 真
                .set(Plaza::getDeleteFlag,Boolean.TRUE)
        );
        //返回结果
        return AjaxResult.success("删除成功");
    }
    
    //增加
    @PostMapping("/insert/Plaza")
    public AjaxResult insertPlaza(@RequestBody Plaza plaza){
        //设置默认值
        plaza.setCreateBy(SecurityUtils.getUsername());
        plaza.setCreateTime(new Date());
        plaza.setUpdateBy(SecurityUtils.getUsername());
        plaza.setUpdateTime(new Date());
        plaza.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        plazaService.save(plaza);
        //返回结果
        return AjaxResult.success("新增成功");
    }

    //修改
    @PostMapping("/update/Plaza")
    public AjaxResult updatePlaza(@RequestBody Plaza plaza){
        //设置默认值
        plaza.setUpdateTime(new Date());
        plaza.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        plazaService.updateById(plaza);
        //返回结果
        return AjaxResult.success("修改成功");
    }
}